package com.ruoyi.web.controller.market.special;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.db.domain.SpecialCostomManagerInfo;
import com.ruoyi.market.enums.ManagerTypeEnum;
import com.ruoyi.special.service.ISpecialCostomManagerInfoService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.wapper.SpecialCostomManagerInfoWapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 机构客户经理信息Controller
 * 
 * @author ruoyi
 * @date 2023-04-19
 */
@Controller
@RequestMapping("/special/managerInfo")
public class SpecialCostomManagerInfoController extends BaseController
{
    private String prefix = "special/managerInfo";

    @Autowired
    private ISpecialCostomManagerInfoService specialCostomManagerInfoService;
    @Autowired
    private ISysUserService sysUserService;

    @RequiresPermissions("special:managerInfo:view")
    @GetMapping("/{type}")
    public String managerInfo(ModelMap modelMap, @PathVariable("type") String type)
    {
        modelMap.put("type", type);
        return prefix + "/managerInfo";
    }

    /**
     * 查询机构客户经理信息列表
     */
    @RequiresPermissions("special:managerInfo:view")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SpecialCostomManagerInfo specialCostomManagerInfo)
    {
        startPage();
        List<Map<String, Object>> mapList = specialCostomManagerInfoService.selectSpecialCostomManagerInfoListMap(specialCostomManagerInfo);
        mapList = new SpecialCostomManagerInfoWapper(mapList).wrap();
        return getDataTable(mapList);
    }

    /**
     * 导出机构客户经理信息列表
     */
    @RequiresPermissions("special:managerInfo:export")
    @Log(title = "机构客户经理信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SpecialCostomManagerInfo specialCostomManagerInfo)
    {
        List<SpecialCostomManagerInfo> list = specialCostomManagerInfoService.selectSpecialCostomManagerInfoList(specialCostomManagerInfo);
        ExcelUtil<SpecialCostomManagerInfo> util = new ExcelUtil<SpecialCostomManagerInfo>(SpecialCostomManagerInfo.class);
        return util.exportExcel(list, "机构客户经理信息数据");
    }

    /**
     * 新增机构客户经理信息
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {

        List<Map<String, Object>> typeMapList = ManagerTypeEnum.getMapList();
        modelMap.put("typeMapList", typeMapList);

        return prefix + "/add";
    }

    /**
     * 新增保存机构客户经理信息
     */
    @RequiresPermissions("special:managerInfo:add")
    @Log(title = "机构客户经理信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SpecialCostomManagerInfo specialCostomManagerInfo)
    {
        SysUser sysUser = sysUserService.selectUserById(specialCostomManagerInfo.getUserId());
        if (ObjectUtil.isEmpty(sysUser)){
            return toAjax(false);
        }

        specialCostomManagerInfo.setUserId(sysUser.getUserId());
        specialCostomManagerInfo.setUsername(sysUser.getUserName());

        return toAjax(specialCostomManagerInfoService.insertSpecialCostomManagerInfo(specialCostomManagerInfo));
    }

    /**
     * 修改机构客户经理信息
     */
    @RequiresPermissions("special:managerInfo:edit")
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        SpecialCostomManagerInfo specialCostomManagerInfo = specialCostomManagerInfoService.selectSpecialCostomManagerInfoByUserId(userId);
        mmap.put("specialCostomManagerInfo", specialCostomManagerInfo);

        List<Map<String, Object>> typeMapList = ManagerTypeEnum.getMapList();
        for (Map<String, Object> map: typeMapList){
            String value = (String) map.get("value");
            if (StrUtil.contains(specialCostomManagerInfo.getType(), value)){
                map.put("flag", true);
            }else{
                map.put("flag", false);
            }
        }
        mmap.put("typeMapList", typeMapList);

        return prefix + "/edit";
    }

    /**
     * 修改保存机构客户经理信息
     */
    @RequiresPermissions("special:managerInfo:edit")
    @Log(title = "机构客户经理信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SpecialCostomManagerInfo specialCostomManagerInfo)
    {
        return toAjax(specialCostomManagerInfoService.updateSpecialCostomManagerInfo(specialCostomManagerInfo));
    }

    /**
     * 删除机构客户经理信息
     */
    @RequiresPermissions("special:managerInfo:remove")
    @Log(title = "机构客户经理信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        SpecialCostomManagerInfo updateEntity = new SpecialCostomManagerInfo();
        updateEntity.setUserId(Long.valueOf(ids));
        updateEntity.setStatus(Long.valueOf(0));
        return toAjax(specialCostomManagerInfoService.updateSpecialCostomManagerInfo(updateEntity));
    }
}
